package com.itheima.web.controller.baseInfo;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.itheiam.service.message.ProductService;
import com.itheima.domain.cargo.Factory;
import com.itheima.domain.cargo.FactoryExample;
import com.itheima.domain.message.ProductExample;
import com.itheima.service.cargo.FactoryService;
import com.itheima.web.controller.BaseController;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping(value = "/baseInfo/factory")
public class FactoryController extends BaseController {

    @Reference
    private FactoryService factoryService;

    @Reference
    private ProductService productService;

    @RequestMapping(value = "/list", name = "分页查询厂家信息列表")
    public String list(@RequestParam(defaultValue = "1") int page,
                       @RequestParam(defaultValue = "5") int size) {
        PageInfo<Factory> pageInfo = factoryService.findAll(page, size, new FactoryExample());
        request.setAttribute("page", pageInfo);
        return "baseInfo/factory/factory-list";
    }

    @RequestMapping(value = "/toAdd", name = "跳转到厂家添加页面")
    public String toAdd() {
        return "baseInfo/factory/factory-add";
    }

    @RequestMapping(value = "/toUpdate", name = "跳转到厂家修改页面")
    public String toUpdate(String id) {
        Factory factory = factoryService.findById(id);
        request.setAttribute("factory", factory);
        return "baseInfo/factory/factory-update";
    }

    @RequestMapping(value = "edit", name = "修改或新增厂家")
    public String edit(Factory factory) {
        if (StringUtils.isEmpty(factory.getId())) {
            factoryService.save(factory);
        } else {
            factoryService.update(factory);
        }
        return "redirect:/baseInfo/factory/list.do";
    }

    @RequestMapping(value = "delete", name = "删除厂家")
    public String delete(String id) {
        factoryService.delete(id);
        return "redirect:/baseInfo/factory/list.do";
    }

    @RequestMapping(value = "toViewDetails", name = "跳转到查看厂家详情页面")
    public String toViewDetails(@RequestParam(defaultValue = "1") int page,
                         @RequestParam(defaultValue = "5") int size,
                         String id) {
        Factory factory = factoryService.findById(id);
        request.setAttribute("factory", factory);

        ProductExample example = new ProductExample();
        ProductExample.Criteria criteria = example.createCriteria();
        criteria.andCompanyIdEqualTo(companyId);
        criteria.andCompanyNameEqualTo(companyName);
        criteria.andFactoryIdEqualTo(id);
        PageInfo pageInfo = productService.findAll(example, page, size);
        request.setAttribute("page", pageInfo);
        return "baseInfo/factory/factoryproduct-list";
    }

    @RequestMapping(value = "toView", name = "跳转到查看厂家页面")
    public String toView(String id) {
        Factory factory = factoryService.findById(id);
        request.setAttribute("factory", factory);
        return "baseInfo/factory/factory-view";
    }
}
